// @Auth:zdl
package bll

import (
	"fmt"
	"sync"

	"gitee.com/sky_big/mylog"
	"github.com/gogf/gf/g/container/garray"
	"github.com/gogf/gf/g/container/gmap"

	"github.com/zhongdalu/DoorValveErWang/model"
)

type CmdModel struct {
	Fac    string
	Dtu    string
	Center string
	MpNo   string
	Mod    int
	Value  int

	model.AddInfo
}

func NewCmdModel(info model.LiveInfo, fkd int, classId, remark string) *CmdModel {
	equip := info.EquipInfo
	houseId := info.HouseInfo
	return &CmdModel{
		Fac:    equip.Fac,
		Dtu:    equip.Dtu,
		Center: equip.Center,
		MpNo:   equip.MpNo,
		Mod:    equip.Mod,
		Value:  fkd,

		AddInfo: model.AddInfo{
			HouseId: houseId,
			ClassId: classId,
			Hswd:    info.HsHisTemp,
			Jswd:    info.JsHisTemp,
			Snwd:    info.Snwd,
			CurrFkd: equip.Fkd,
			NeedFkd: fkd,
			Remarks: remark,
		},
	}
}

type HouseModel struct {
	maps *gmap.StrAnyMap
}

func NewHouseModel() *HouseModel {
	return &HouseModel{
		maps: gmap.NewStrAnyMap(),
	}
}

func (r *HouseModel) ResetComplete() {
	r.maps.Iterator(func(k string, v interface{}) bool {
		if val, ok := v.(*HouseInfoModel); ok {
			if val.warning == -1 {
				val.warning = 0
			}
		}
		return true
	})
}

func (r *HouseModel) SetValue(key string, model *HouseInfoModel) {
	r.maps.Set(key, model)
}

func (r *HouseModel) GetValue(key string) *HouseInfoModel {
	if r.IsExist(key) == false {
		return nil
	}
	return r.maps.Get(key).(*HouseInfoModel)
}

func (r *HouseModel) IsExist(key string) bool {
	return r.maps.Contains(key)
}

func (r *HouseModel) SetIfNotExist(key string, value *HouseInfoModel) bool {
	return r.maps.SetIfNotExist(key, value)
}

func (r *HouseModel) IsComplete() bool {
	complete := true
	r.maps.Iterator(func(k string, v interface{}) bool {
		if val, ok := v.(*HouseInfoModel); ok {
			if val.warning == 0 {
				complete = false
				return false
			}
		}
		return true
	})
	return complete
}

func (r *HouseModel) GetFinishNum() int {
	num := 0
	r.maps.Iterator(func(k string, v interface{}) bool {
		if val, ok := v.(*HouseInfoModel); ok {
			if val.warning != 0 {
				num++
			}
		}
		return true
	})
	return num
}

func (r *HouseModel) GetSuccessNum() int {
	num := 0
	r.maps.Iterator(func(k string, v interface{}) bool {
		if val, ok := v.(*HouseInfoModel); ok {
			if val.warning == -1 {
				num++
			}
		}
		return true
	})
	return num
}

type HouseInfoModel struct {
	mu           *sync.RWMutex
	id           string
	uuid         string
	warnCountArr *garray.IntArray
	warning      int // 0 未判断  1已报警 -1 正常
}

func NewHouseInfoModel(id string) *HouseInfoModel {
	return &HouseInfoModel{
		mu:           new(sync.RWMutex),
		id:           id,
		warnCountArr: garray.NewIntArrayFrom([]int{0, 0, 0, 0, 0, 0, 0, 0}),
	}
}

func (r *HouseInfoModel) SetWarning(i int) {
	r.mu.RLock()
	defer r.mu.RUnlock()
	r.warning = i
}

func (r *HouseInfoModel) GetWarning() int {
	r.mu.RLock()
	defer r.mu.RUnlock()
	return r.warning
}

func (r *HouseInfoModel) GetWarnCount(index int) int {
	r.mu.RLock()
	defer r.mu.RUnlock()
	if index > r.warnCountArr.Len()-1 {
		mylog.Error(fmt.Sprintf("%d 超出范围", index))
		return 0
	}
	return r.warnCountArr.Get(index)
}

func (r *HouseInfoModel) AddWarnCount(index int) {
	r.mu.RLock()
	defer r.mu.RUnlock()
	if index > r.warnCountArr.Len()-1 {
		mylog.Error(fmt.Sprintf("%d 超出范围", index))
		return
	}
	value := r.warnCountArr.Get(index)
	value++
	r.warnCountArr.Set(index, value)
}

type WarnModel struct {
	Type     int
	WarnType int
	HouseId  string
	ClassId  string
}

type OperaResultInfo struct {
	HouseInfo string  `json:"house_info"`
	ClassId   string  `json:"uuid"` // 分拨标识
	CurrFkd   int     `json:"curr_fkd"`
	NeedFkd   int     `json:"need_fkd"`
	Jswd      float64 `json:"jswd"`
	Hswd      float64 `json:"hswd"`
	Snwd      float64 `json:"snwd"`
	Remarks   string  `json:"remarks"`
	Error     string  `json:"error"`
	Errno     int     `json:"errno"`
}
